/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package core.threads.server.communication;
import core.messages.Msg;
import core.threads.server.Server_Public_MsgBox;
import myTest.MYTest;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Invince
 */
public class T_Server_Receiver extends Thread {
/*----------Attributs-------------------*/
    protected boolean stop;
    protected boolean suspend;

    protected ServerSocket serverSocket;//监听端口
    protected int listenPort;
    protected Socket socket;
    protected ObjectInputStream objInPut;
/*----------Public functions------------*/
    @Override
    public void run() {
        try {
            /**-----Init-----**/
            /**-----Loop----**/
            while (!this.stop) {
                if(!this.suspend){
                    MYTest.println("Thread:T_Server_Receiver start");
                    this.socket = this.serverSocket.accept();
                    this.objInPut=new ObjectInputStream(this.socket.getInputStream());
                    MYTest.println("Thread:T_Server_Receiver accept");

                    MYTest.println("Thread:T_Server_Receiver Waiting Recevie");
                    Msg msg = receive();
                    MYTest.println("Thread:T_Server_Receiver Receive Success");
                    Server_Public_MsgBox.msgBox_Server_Rcv_Msg.offer(msg);
                }
            }
            /**----End-----**///这里End部分写在下面的finally里了
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(T_Server_Receiver.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(T_Server_Receiver.class.getName()).log(Level.SEVERE, null, ex);
        } finally{
            try {
                this.objInPut.close();//注意如果在receive里open close的话，会把socket一起close掉
                this.socket.close();
                this.serverSocket.close();
            } catch (IOException ex) {
                Logger.getLogger(T_Server_Receiver.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public Msg receive() throws IOException, ClassNotFoundException{
        Msg r= (Msg)this.objInPut.readObject();
        MYTest.println("A message received. ");
        return r;
    }

    public void stopT(){
        synchronized(this){
            this.stop=true;
        }
    }

    public void suspendT(){
        synchronized(this){
            this.suspend=true;
        }
    }

    public void resumeT(){
        synchronized(this){
            this.suspend=false;
        }
    }
    
/*----------Private functions-----------*/
/*----------Constructor-----------------*/
    public T_Server_Receiver(int listenPort) throws IOException {
        synchronized(this){
            this.stop=false;
            this.suspend=false;
            this.listenPort=listenPort;
            this.serverSocket=new ServerSocket(listenPort);
            MYTest.println("Thread:T_Server_Receiver created");
        }
    }

/*----------Gets and sets---------------*/
    public int getListenPort() {
        return listenPort;
    }

    public void setListenPort(int listenPort) {
        this.listenPort = listenPort;
    }

    public ServerSocket getServerSocket() {
        return serverSocket;
    }

    public void setServerSocket(ServerSocket serverSocket) {
        this.serverSocket = serverSocket;
    }

    public Socket getSocket() {
        return socket;
    }

    public void setSocket(Socket socket) {
        this.socket = socket;
    }

    public ObjectInputStream getObjInPut() {
        return objInPut;
    }

    public void setObjInPut(ObjectInputStream objInPut) {
        this.objInPut = objInPut;
    }

}
